package com.bootcamp.javase2.synchro;

/**
 * @program: day20Sycn
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-14 21:20
 **/

public class PrintLambda {
    static Object obj = new Object();
    static boolean toPrintOdd = true;

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (obj) {
                for (int i = 1; i < 10000; i += 2) {
//                    if (toPrintOdd){
//                        System.out.println(Thread.currentThread().getName()+"----"+i);
//                        toPrintOdd = false;
//                        obj.notify();
//                        try {
//                            obj.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
                    System.out.println(Thread.currentThread().getName() + "----" + i);
                    obj.notify();
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "ThreadOdd").start();

        new Thread(() -> {
            synchronized (obj){
                for (int i = 2; i <= 100; i+=2) {
                    System.out.println(Thread.currentThread().getName() + "----" + i);
                    obj.notify();
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "ThreadEven").start();
    }
}
